// src/stores/domainStore.ts
import { create } from 'zustand';
import { domainsApi } from '../api/domains';
import { Domain, DomainStats } from '../api/types';

interface DomainStore {
  domains: Domain[];
  selectedDomain: Domain | null;
  domainStats: Map<string, DomainStats>;
  loading: boolean;
  error: string | null;

  // Actions
  fetchDomains: (connectionId?: string) => Promise<void>;
  fetchDomain: (name: string, connectionId?: string) => Promise<void>;
  selectDomain: (domain: Domain | null) => void;
  startDomain: (name: string, connectionId?: string) => Promise<void>;
  shutdownDomain: (name: string, connectionId?: string) => Promise<void>;
  forceStopDomain: (name: string, connectionId?: string) => Promise<void>;
  rebootDomain: (name: string, connectionId?: string) => Promise<void>;
  suspendDomain: (name: string, connectionId?: string) => Promise<void>;
  resumeDomain: (name: string, connectionId?: string) => Promise<void>;
  deleteDomain: (name: string, connectionId?: string) => Promise<void>;
  updateDomainStats: (name: string, stats: DomainStats) => void;
  setLoading: (loading: boolean) => void;
  setError: (error: string | null) => void;
}

export const useDomainStore = create<DomainStore>((set, get) => ({
  domains: [],
  selectedDomain: null,
  domainStats: new Map(),
  loading: false,
  error: null,

  fetchDomains: async (connectionId) => {
    set({ loading: true, error: null });
    try {
      const domains = await domainsApi.list(connectionId);
      set({ domains, loading: false });
    } catch (error: any) {
      set({ error: error.message, loading: false });
    }
  },

  fetchDomain: async (name, connectionId) => {
    try {
      const domain = await domainsApi.get(name, connectionId);
      set({ selectedDomain: domain });
    } catch (error: any) {
      set({ error: error.message });
    }
  },

  selectDomain: (domain) => set({ selectedDomain: domain }),

  startDomain: async (name, connectionId) => {
    try {
      await domainsApi.start(name, connectionId);
      await get().fetchDomains(connectionId);
    } catch (error: any) {
      set({ error: error.message });
      throw error;
    }
  },

  shutdownDomain: async (name, connectionId) => {
    try {
      await domainsApi.shutdown(name, connectionId);
      await get().fetchDomains(connectionId);
    } catch (error: any) {
      set({ error: error.message });
      throw error;
    }
  },

  forceStopDomain: async (name, connectionId) => {
    try {
      await domainsApi.forceStop(name, connectionId);
      await get().fetchDomains(connectionId);
    } catch (error: any) {
      set({ error: error.message });
      throw error;
    }
  },

  rebootDomain: async (name, connectionId) => {
    try {
      await domainsApi.reboot(name, connectionId);
      await get().fetchDomains(connectionId);
    } catch (error: any) {
      set({ error: error.message });
      throw error;
    }
  },

  suspendDomain: async (name, connectionId) => {
    try {
      await domainsApi.suspend(name, connectionId);
      await get().fetchDomains(connectionId);
    } catch (error: any) {
      set({ error: error.message });
      throw error;
    }
  },

  resumeDomain: async (name, connectionId) => {
    try {
      await domainsApi.resume(name, connectionId);
      await get().fetchDomains(connectionId);
    } catch (error: any) {
      set({ error: error.message });
      throw error;
    }
  },

  deleteDomain: async (name, connectionId) => {
    try {
      await domainsApi.delete(name, connectionId);
      await get().fetchDomains(connectionId);
    } catch (error: any) {
      set({ error: error.message });
      throw error;
    }
  },

  updateDomainStats: (name, stats) => {
    set((state) => {
      const newStats = new Map(state.domainStats);
      newStats.set(name, stats);
      return { domainStats: newStats };
    });
  },

  setLoading: (loading) => set({ loading }),
  setError: (error) => set({ error }),
}));

